/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cpsc315project3;

/**
 *
 * @author Dustin Rieke
 */

import java.io.*;
import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.*;
import org.w3c.dom.traversal.*;
import org.xml.sax.helpers.DefaultHandler;
import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.SAXException;

public class HelperMethods 
{
    TravelJournal tj;
    DOMParser parser;
    public HelperMethods()
    {
        
    }
    public Document createParser(String str)
    {
        Document doc = null;
        try 
        {
            parser = new DOMParser();
            parser.parse(str);
            doc = parser.getDocument();      
        }
        catch(Exception ex)
        {
            System.out.println(ex);
        }
        return doc;
    }
    public void parseFile(Document doc)
    {
        DocumentTraversal traversable = (DocumentTraversal) doc;
        NodeIterator iter = traversable.createNodeIterator(
                doc, NodeFilter.SHOW_ALL, null, true);
        Node node;
        node = iter.getRoot();
        while((node = iter.nextNode()) != null)
        {
            if(node.getNodeName().equals("TRAVELJOURNAL"))
            {
                tj = new TravelJournal();
                continue;
            }
            if(node.getNodeName().equals("Picture"))
            {
                Picture pic = new Picture();
                NodeList pictureElements = node.getChildNodes();
                for(int i=0;i<pictureElements.getLength();i++)
                {
                    if(pictureElements.item(i).getNodeName().equals("Photo"))
                    {
                        pic.Photo = pictureElements.item(i).getTextContent();
                    }
                    if(pictureElements.item(i).getNodeName().equals("Date"))
                    {
                        pic.Date = pictureElements.item(i).getTextContent();
                    }
                    if(pictureElements.item(i).getNodeName().equals("Location"))
                    {
                        pic.Location = pictureElements.item(i).getTextContent();
                    }
                    if(pictureElements.item(i).getNodeName().equals("Place"))
                    {
                        pic.Place = pictureElements.item(i).getTextContent();
                    }
                    if(pictureElements.item(i).getNodeName().equals("Person"))
                    {
                        pic.People.add(pictureElements.item(i).getTextContent());
                    }
                    if(pictureElements.item(i).getNodeName().equals("Activity"))
                    {
                        pic.Activity = pictureElements.item(i).getTextContent();
                    }
                    if(pictureElements.item(i).getNodeName().equals("Food"))
                    {
                        pic.Food.add(pictureElements.item(i).getTextContent());
                    }
                    if(pictureElements.item(i).getNodeName().equals("TimeOfDay"))
                    {
                        pic.TimeOfDay = pictureElements.item(i).getTextContent();
                    }
                    if(pictureElements.item(i).getNodeName().equals("Expenses"))
                    {
                        pic.Expenses.add(pictureElements.item(i).getTextContent());
                    }
                    if(pictureElements.item(i).getNodeName().equals("Album"))
                    {
                        pic.Album.add(pictureElements.item(i).getTextContent());
                    }
                    if(pictureElements.item(i).getNodeName().equals("text"))
                    {
                        pic.Text.add(pictureElements.item(i).getTextContent());
                    }
                    if(pictureElements.item(i).getNodeName().equals("TravelPlans"))
                    {
                        NodeList tpElements = pictureElements.item(i).getChildNodes();
                        for(int j=0;j<tpElements.getLength();j++)
                        {
                            if(tpElements.item(j).getNodeName().equals("TravelPlansMakeup"))
                            {
                                TravelPlansMakeup tpm = new TravelPlansMakeup();
                                NodeList tpmElements = tpElements.item(j).getChildNodes();
                                for(int k=0;k<tpmElements.getLength();k++)
                                {
                                    if(tpmElements.item(k).getNodeName().equals("ModeOfTransportation"))
                                    {
                                        tpm.ModeOfTransportation = tpmElements.item(k).getTextContent();
                                    }
                                    if(tpmElements.item(k).getNodeName().equals("TimeDepart"))
                                    {
                                        tpm.TimeDepart = tpmElements.item(k).getTextContent();
                                    }
                                    if(tpmElements.item(k).getNodeName().equals("TimeArrive"))
                                    {
                                        tpm.TimeArrive = tpmElements.item(k).getTextContent();
                                    }
                                    if(tpmElements.item(k).getNodeName().equals("Location"))
                                    {
                                        tpm.Location = tpmElements.item(k).getTextContent();
                                    }
                                    if(tpmElements.item(k).getNodeName().equals("Place"))
                                    {
                                        tpm.Place = tpmElements.item(k).getTextContent();
                                    }
                                    if(tpmElements.item(k).getNodeName().equals("DurationOfTravel"))
                                    {
                                        tpm.DurationOfTravel = tpmElements.item(k).getTextContent();
                                    }
                                }
                                pic.TravPlans.makeup.add(tpm);
                                continue;
                            }
                        }
                    }
                }
                tj.Pictures.add(pic);
                continue;
            }
            if(node.getNodeName().equals("TravelPlans") && node.getParentNode().getNodeName().equals("TRAVELJOURNAL"))
            {
                NodeList tpElements = node.getChildNodes();
                for(int l=0;l<tpElements.getLength();l++)
                {
                    if(tpElements.item(l).getNodeName().equals("TravelPlansMakeup"))
                    {
                        TravelPlansMakeup tpm = new TravelPlansMakeup();
                        NodeList tpmElements = tpElements.item(l).getChildNodes();
                        for(int m=0;m<tpmElements.getLength();m++)
                        {
                            if(tpmElements.item(m).getNodeName().equals("ModeOfTransportation"))
                            {
                                tpm.ModeOfTransportation = tpmElements.item(m).getTextContent();
                            }
                            if(tpmElements.item(m).getNodeName().equals("TimeDepart"))
                            {
                                tpm.TimeDepart = tpmElements.item(m).getTextContent();
                            }
                            if(tpmElements.item(m).getNodeName().equals("TimeArrive"))
                            {
                                tpm.TimeArrive = tpmElements.item(m).getTextContent();
                            }
                            if(tpmElements.item(m).getNodeName().equals("Location"))
                            {
                                tpm.Location = tpmElements.item(m).getTextContent();
                            }
                            if(tpmElements.item(m).getNodeName().equals("Place"))
                            {
                                tpm.Place = tpmElements.item(m).getTextContent();
                            }
                            if(tpmElements.item(m).getNodeName().equals("DurationOfTravel"))
                            {
                                tpm.DurationOfTravel = tpmElements.item(m).getTextContent();
                            }
                        }
                        tj.Plans.makeup.add(tpm);
                        continue;
                    }
                }
            }
            if(node.getNodeName().equals("Expenses") && node.getParentNode().getNodeName().equals("TRAVELJOURNAL"))
            {
                tj.Expenses.add(node.getTextContent());                
            }
            if(node.getNodeName().equals("text") && node.getParentNode().getNodeName().equals("TRAVELJOURNAL"))
            {
                tj.Text.add(node.getTextContent());
            }
        }
    }
    public void reportResults()
    {
        System.out.println("There are " + tj.Pictures.size() + " pictures in the Travel Journal");
        for(int i=0;i<tj.Pictures.size();i++)
        {
            System.out.println("The attributes for picture number " + (i+1) + " are:");
            System.out.println("Photo:" + tj.Pictures.get(i).Photo);
            System.out.println("Date:" + tj.Pictures.get(i).Date);
            System.out.println("Location:" + tj.Pictures.get(i).Location);
            System.out.println("Place:" + tj.Pictures.get(i).Place);
            System.out.println("Activity:" + tj.Pictures.get(i).Activity);
            for(int j=0;j<tj.Pictures.get(i).People.size();j++)
            {
                System.out.println("Person: " + tj.Pictures.get(i).People.get(j));
            }
            for(int j=0;j<tj.Pictures.get(i).Food.size();j++)
            {
                System.out.println("Food: " + tj.Pictures.get(i).Food.get(j));
            }
            System.out.println("TimeOfDay" + tj.Pictures.get(i).TimeOfDay);
            
            System.out.println("There are " + tj.Pictures.get(i).TravPlans.makeup.size()
                    + " TravelPlansMakeup objects for this picture: ");
            for(int k=0;k<tj.Pictures.get(i).TravPlans.makeup.size();k++)
            {
                System.out.println("The attributes for TravelPlansMakeup object " + (k+1) + " are:");
                System.out.println("ModeOfTransportation: " + 
                        tj.Pictures.get(i).TravPlans.makeup.get(k).ModeOfTransportation);
                System.out.println("TimeDepart: " + 
                        tj.Pictures.get(i).TravPlans.makeup.get(k).TimeDepart);
                System.out.println("TimeArrive: " + 
                        tj.Pictures.get(i).TravPlans.makeup.get(k).TimeArrive);
                System.out.println("Location: " + 
                        tj.Pictures.get(i).TravPlans.makeup.get(k).Location);
                System.out.println("Place: " + 
                        tj.Pictures.get(i).TravPlans.makeup.get(k).Place);
                System.out.println("DurationOfTravel: " + 
                        tj.Pictures.get(i).TravPlans.makeup.get(k).DurationOfTravel);
            }            
            for(int j=0;j<tj.Pictures.get(i).Expenses.size();j++)
            {
                System.out.println("Expenses: " + tj.Pictures.get(i).Expenses.get(j));
            }
            for(int j=0;j<tj.Pictures.get(i).Album.size();j++)
            {
                System.out.println("Album: " + tj.Pictures.get(i).Album.get(j));
            }
            for(int j=0;j<tj.Pictures.get(i).Text.size();j++)
            {
                System.out.println("Text: " + tj.Pictures.get(i).Text.get(j));
            }
        }
        System.out.println("There are " + tj.Plans.makeup.size()
                + " TravelPlansMakeup objects for this Travel Journal: ");
        for(int k=0;k<tj.Plans.makeup.size();k++)
        {
            System.out.println("The attributes for TravelPlansMakeup object " + (k+1) + " are:");
            System.out.println("ModeOfTransportation: " + 
                    tj.Plans.makeup.get(k).ModeOfTransportation);
            System.out.println("TimeDepart: " + 
                    tj.Plans.makeup.get(k).TimeDepart);
            System.out.println("TimeArrive: " + 
                    tj.Plans.makeup.get(k).TimeArrive);
            System.out.println("Location: " + 
                    tj.Plans.makeup.get(k).Location);
            System.out.println("Place: " + 
                    tj.Plans.makeup.get(k).Place);
            System.out.println("DurationOfTravel: " + 
                    tj.Plans.makeup.get(k).DurationOfTravel);
        }

        for(int j=0;j<tj.Text.size();j++)
        {
            System.out.println("Text: " + tj.Text.get(j));
        }

        for(int j=0;j<tj.Expenses.size();j++)
        {
            System.out.println("Expenses: " + tj.Expenses.get(j));
        }
    }
}
